<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use App\Models\AiModel;
use App\Models\Words;
use App\Models\Role;
class ApiController extends Controller{
	
    /*
     1、type 预设ID
     2、input  输入
     3、temperature 
     4、mid  模型ID
     5、uid   用户ID
     6、passwd  使用密码
     */
	public function chat(Request $req){
	    $data = json_decode($req->getContent(),true);
	    $type = $data['type']??0;//类型
	    $input = $data['input']??'';//输入
	    $temperature = isset($data['temperature']) && $data['temperature'] ? $data['temperature'] : 0.7;
	    $mid = $data['mid']??0;
	    $uid = $data['uid']??'';
	    $passwd = $data['passwd']??'';
	    if ($passwd != 'aizs') wxresp(0,'passwd错误');
	    if (empty($mid)) wxresp(0,'mid错误');
	    $model = AiModel::find($mid);
	    if (empty($model)) wxresp(0,'mid错误');
	    $role = Role::find($type);
	    if (empty($role)) wxresp(0,'type错误');
	    if (empty($input)) wxresp(0,'input不能为空');
	    $words = array(array('role'=>'system','content'=>$role->des));
        if ($uid) {
            $word = Words::where('uid',$uid)->orderByDesc('id')->get()->toArray();
            if ($word) {
                foreach ($word as $value) {
                    $words[] = array('role'=>'user','content'=>$value['ask']);
                    $words[] = array('role'=>'assistant','content'=>$value['resp']);
                }
            }
        }
        $words[] = array('role'=>'user','content'=>$input);
	    $url = "https://ai98.vip/v1/chat/completions";
	    $post = array(
	            'model'=> $model->model,
	            'messages'=>$words,
	            "temperature"=> floatval($temperature),
	            "top_p"=>1,
	            "frequency_penalty"=>0,
	            "presence_penalty"=>0.6,
	    );
	    $header = array(
	            "Authorization: Bearer ".$model->appkey,
	            "Content-Type: application/json",
	    );
	    $curl = curl_init();
	    curl_setopt($curl, CURLOPT_URL, $url);
	    curl_setopt($curl, CURLOPT_HEADER, 0);
	    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
	    curl_setopt($curl, CURLOPT_POST, 1);
	    curl_setopt($curl, CURLOPT_POSTFIELDS,json_encode($post,JSON_UNESCAPED_UNICODE));
	    $res = curl_exec($curl);
	    curl_close($curl);
	    $res = json_decode($res,1);
	    if (isset($res['choices'][0]['message']['content'])) {
	        $content = $res['choices'][0]['message']['content'];
	        if ($uid) Words::insert(array('mid'=>$model->id,'uid'=>$uid,'ask'=>$input,'resp'=>$content,'createtime'=>time()));
	        wxresp(1,array('data'=>$content));
	    }else wxresp(0,$res['error']['message']);
	}
	/*
    type  翻译类型 如英-中传(en-zh)
    content 翻译内容
    passwd 密码
	 */
	public function translate(Request $req){
	    $data = json_decode($req->getContent(),true);
	    if (!isset($data['type']) || empty($data['type'])) wxresp(0,'翻译类型必填');
	    if (!isset($data['content']) || empty($data['content'])) wxresp(0,'翻译内容必填');
	    $passwd = $data['passwd']??'';
	    if ($passwd != 'aizs') wxresp(0,'passwd错误');
	    $url = "https://api.newtranx.com/newtranx-api/open/textBatch/translate";
	    $post = array(
	            'appkey'=>'WeKIDTT3',
	            'direction'=>$data['type'],
	            'common'=>'common',
	            'text'=>[$data['content']],
	    );
	    $post['sign'] = md5("common={$post['common']}&direction={$post['direction']}&text=".implode("", $post['text'])."&214296ec260f18d66b029292c9a2764957733a77");
	    $post['str'] = "common={$post['common']}&direction={$post['direction']}&text=".implode("", $post['text'])."&214296ec260f18d66b029292c9a2764957733a77";
	    $header = array(
	            "Content-Type: application/json",
	    );
	    $curl = curl_init();
	    curl_setopt($curl, CURLOPT_URL, $url);
	    curl_setopt($curl, CURLOPT_HEADER, 0);
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
	    curl_setopt($curl, CURLOPT_POST, 1);
	    curl_setopt($curl, CURLOPT_POSTFIELDS,json_encode($post,JSON_UNESCAPED_UNICODE));
	    $res = curl_exec($curl);
	    curl_close($curl);
	    Log::info(json_encode($post));
	    Log::info($res);
	    $res = json_decode($res,1);
	    if ($res['resultCode'] == 200) wxresp(1,array('data'=>$res['data']['translation'][0]['translated'][0]['text']));
	    else wxresp(0,'接口错误:'.$res['resultMessage']);
	}
}